package com.example.GeoCoder;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.Document;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

public class ClientTestActivity extends Activity {

	static TextView myLatitude;
	static TextView myLongitude;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	//	myLatitude = (TextView) findViewById(R.id.mylatitude);
		//myLongitude = (TextView) findViewById(R.id.mylongitude);
		// myLatitude.setText("Latitude123: ");
		connectSocket();
	}

	private void connectSocket() {
		PrintWriter out = null;
		BufferedReader in = null;
		// TODO Auto-generated method stub
		try {
			InetAddress serverAddr = InetAddress.getByName("192.168.2.121");// 10.66.3.44

			Log.d("TCP", "C: Connecting...");
			Socket socket = new Socket(serverAddr, 8221);
			String message = "Winnet";
			try {
				out = new PrintWriter(socket.getOutputStream(), true);
				in = new BufferedReader(new InputStreamReader(
						socket.getInputStream()));
				myLongitude.setText("sentmessage");
			} catch (UnknownHostException e) {
				System.err.println("Don't know about host: taranis.");
				System.exit(1);
			} catch (IOException e) {
				System.err.println("Couldn't get I/O for "
						+ "the connection to: taranis.");
				System.exit(1);
			}

			/*
			 * in.readLine(); if ( message != null) { out.println(message);
			 * //XMLOutputter outputter = new XMLOutputter();
			 * //outputter.output((org.jdom.Document) doce, System.out);
			 * myLatitude.setText("recieved:"+in.readLine());
			 * 
			 * }
			 */
			if (message != null) {
				out.println(message);
				in.readLine();
				// Document doc = in;
				// String s= doc.getElementById("address").getTextContent();
				// myLatitude.setText("recieved:"+in.readLine());

				// Read the entire XML in one string and pass this
				// as a String Reader to DOM
				StringReader sr = new StringReader(in.readLine());
				SAXBuilder builder = new SAXBuilder();
				Document doc = builder.build(sr);

				// Get the name of the root element and return this to the
				// client
				// as a response.
				//Element element = doc.getRootElement();
				//String rootName = element.getName();
				Element addElement = doc.getRootElement().getChild("address");
				String addr = addElement.getText();
				myLatitude.setText("recieved:" + addr);
				// out.println(rootName);

			}

			out.close();
			in.close();
			// stdIn.close();
			socket.close();

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			Log.e("TCP", "C: UnknownHostException", e);
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e("TCP", "C: IOException", e);
			e.printStackTrace();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
